package org;
import org.entities.LevelObject;
import org.entities.Block;
import org.entities.Player;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
import java.util.ArrayList;

public class BlockMap {

    public static int mapWidth;
    public static int mapHeight;
    public static TiledMap tmap;
    public static LevelObject[][] blockAt;
    public ArrayList<LevelObject> entities;
    public Player Samus;
    private int dUp1[] = {0, 16, 16, 8, 16, 16, 0, 16}; // diag smooth
    private int dUp2[] = {0, 8, 16, 0, 16, 16, 0, 16};   // diag smooth
    private int dDown1[] = {0, 0, 16, 8, 16, 16, 0, 16};  // diag smooth
    private int dDown2[] = {0, 8, 16, 16, 0, 16, 0, 8};   // diag smooth
    private int hDiag1[] = {0, 16, 16, 0, 16, 16, 0, 16}; // diag hard
    private int hDiag2[] = {0, 0, 16, 16, 0, 16, 0, 0};   // diag hard
    private int square[] = {0, 0, 16, 0, 16, 16, 0, 16};  // square
    private int sDiag1[] = {0, 0, 16, 0, 16, 8, 0, 0};    // diag smooth
    private int sDiag2[] = {0, 0, 16, 0, 16, 16, 0, 8};   // diag smooth
    private int sDiag3[] = {0, 0, 16, 0, 16, 8, 0, 16};   // diag smooth
    private int sDiag4[] = {0, 0, 16, 0, 0, 8, 0, 0};     // diag smooth
    private int hDiag3[] = {0, 0, 16, 0, 16, 16, 0, 0};   // diag hard
    private int hDiag4[] = {0, 0, 16, 0, 0, 16, 0, 0};   // diag hard

    public BlockMap(String ref) throws SlickException {

        tmap = new TiledMap(ref, "data");
        System.out.printf("Current map is =[%s]\n", ref);

        mapWidth = tmap.getWidth() * tmap.getTileWidth();
        mapHeight = tmap.getHeight() * tmap.getTileHeight();

        entities = new ArrayList<LevelObject>();
        blockAt = new LevelObject[tmap.getWidth()][tmap.getHeight()];

        for (int x = 0; x < tmap.getWidth(); x++) {
            for (int y = 0; y < tmap.getHeight(); y++) {
                int tileID = tmap.getTileId(x, y, 0);
                int tileID2 = tmap.getTileId(x, y, 5);


                //create player 
                if (tileID2 == 49) {
                    entities.add(Samus = new Player(x * 16, y * 16, "samus"));
                }

                // create tiles
                if (tileID == 1) {
                    blockAt[x][y] = new Block(x * 16, y * 16, dUp1, "dUp");
                }
                if (tileID == 2) {
                    blockAt[x][y] = new Block(x * 16, y * 16, dUp2, "dUp");
                }
                if (tileID == 3) {
                    blockAt[x][y] = new Block(x * 16, y * 16, dDown1, "dDown");
                }
                if (tileID == 4) {
                    blockAt[x][y] = new Block(x * 16, y * 16, dDown2, "dDown");
                }
                if (tileID == 5) {
                    blockAt[x][y] = new Block(x * 16, y * 16, hDiag1, "hDiag1");
                }
                if (tileID == 6) {
                    blockAt[x][y] = new Block(x * 16, y * 16, hDiag2, "hDiag2");
                }
                if (tileID == 7) {
                    blockAt[x][y] = new Block(x * 16, y * 16, square, "square");
                }
                if (tileID == 17) {
                    blockAt[x][y] = new Block(x * 16, y * 16, sDiag1, "sDiag1");
                }
                if (tileID == 18) {
                    blockAt[x][y] = new Block(x * 16, y * 16, sDiag2, "sDiag2");
                }
                if (tileID == 19) {
                    blockAt[x][y] = new Block(x * 16, y * 16, sDiag3, "sDiag3");
                }
                if (tileID == 20) {
                    blockAt[x][y] = new Block(x * 16, y * 16, sDiag4, "sDiag4");
                }
                if (tileID == 21) {
                    blockAt[x][y] = new Block(x * 16, y * 16, hDiag3, "hdiag3");
                }
                if (tileID == 22) {
                    blockAt[x][y] = new Block(x * 16, y * 16, hDiag4, "hDiag4");
                }
            }
        }

        // when loading a new map,start rendering according to player's location
        GameState.mapX = 200 - Samus.x;
        GameState.mapY = 150 - Samus.y;

        if (GameState.mapX > 0) {
            GameState.mapX = 0;
        }
        if (GameState.mapY > 0) {
            GameState.mapY = 0;
        }
        if (GameState.mapX < -(mapWidth - GameState.viewWidth)) {
            GameState.mapX = -(mapWidth - GameState.viewWidth);
        }
        if (GameState.mapY < -(mapHeight - GameState.viewHeight)) {
            GameState.mapY = -(mapHeight - GameState.viewHeight);
        }
    }

    
    // return a  block if found at location X,Y
    public static LevelObject getBlockAt(int x, int y) {
        return blockAt[x][y];
    }
} 